package com.augurit.common.system.service.org.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.augurit.common.dao.AcUserShortcutDao;
import com.augurit.common.entity.AcUserShortcut;
import com.augurit.common.system.convert.AcUserShortcutConverter;
import com.augurit.common.system.service.org.IAcUserShortcutService;
import com.augurit.common.system.web.org.form.AcUserShortcutForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.StringUtils;

/**
 * 应用逻辑实现类
 */
@Service
@Transactional
public class AcUserShortCutServiceImpl implements IAcUserShortcutService {
	
	// DAO 引用
	@Autowired
	private AcUserShortcutDao acUserShortcutDao; //应用DAO类

	/**
	 * 根据ID获取应用Form对象
	 * @param id 应用主键ID
	 * @return 应用Form对象
	 */
	@Transactional(readOnly = true)
	public AcUserShortcutForm get(Long id){
		return acUserShortcutDao.getForm(id);
	}

	/**
	 * 保存应用Form对象数组
	 * @param forms Form对象数组
	 */
	public void save(AcUserShortcutForm... forms){
		if(forms != null)
			for(AcUserShortcutForm form : forms)
				acUserShortcutDao.save(form);
	}

	/**
	 * 根据主键ID集合删除应用对象
	 * @param ids 应用主键ID集合
	 */
	public void delete(Long... ids) {
		if(ids != null)
			acUserShortcutDao.delete(ids);
	}
	
	/**
	 * 插入快捷菜单到某个用户所有快捷菜单最前面
	 * @param userId
	 * @param menuIds
	 */
	public void insertAtFirst(Long userId, Long...menuIds){
		if(userId != null && menuIds != null && menuIds.length > 0){
			acUserShortcutDao.insertAtFirst(userId, menuIds);
		}
	}
	
	/**
	 * 插入快捷菜单到某个用户所有快捷菜单最后面
	 * @param userId
	 * @param menuIds
	 */
	public void insertAtLast(Long userId, Long...menuIds){
		if(userId != null && menuIds != null && menuIds.length > 0){
			acUserShortcutDao.insertAtLast(userId, menuIds);
		}
	}
	
	/**
	 * 从中间插入快捷菜单
	 * @param userId 用户id
	 * @param pos 插入的位置
	 * @param menuIds 快捷菜单列表
	 */
	public void insertAtMiddle(Long userId, int pos, Long...menuIds){
		if(userId != null && pos >= 0 && menuIds != null && menuIds.length > 0){
			acUserShortcutDao.insertAtMiddle(userId, pos, menuIds);
		}
	}
	
	/**
	 * 清空指定用户的所有快捷菜单
	 * @param userId 
	 */
	public void clearShortcut(Long userId){
		if(userId != null){
			List<AcUserShortcut> entities = acUserShortcutDao.findBy("userId", userId);
			if(entities != null && entities.size() > 0){
				acUserShortcutDao.delete(entities);
			}
		}
	}
	
	/**
	 * 删除用户的快捷菜单
	 * @param userId 用户id
	 * @param menuIds 对应的菜单id
	 */
	public void deleteShortcutFromUser(Long userId, Long...menuIds){
		List<AcUserShortcut> entities = acUserShortcutDao.getByUserIdAndMenuIds(userId, menuIds);
		if(entities != null && entities.size() > 0){
			for(AcUserShortcut entity:entities){
				acUserShortcutDao.upward(userId, entity.getSortNo() - 1, 1);
				acUserShortcutDao.delete(entity);
			}
		}
	}
	
	/**
	 * 删除一批用户对快捷菜单的使用
	 * @param menuId 
	 * @param userIds
	 */
	public void deleteUsersFromShortcut(Long menuId, Long... userIds){
		List<AcUserShortcut> entities = acUserShortcutDao.getByMenuIdAndUserIds(menuId, userIds);
		if(entities != null && entities.size() > 0){
			for(AcUserShortcut entity:entities){
				acUserShortcutDao.delete(entity);
			}
		}
	}
	
	/**
	 * 获取指定用户拥有的快捷菜单id数组
	 * @param userId 用户id
	 * @return 菜单id数组
	 */
	public Long[] getShortcutIdsByUserId(Long userId){
		if(userId != null){
			return acUserShortcutDao.getShortcutIdsByUserId(userId);
		}
		return null;
	}
	
	/**
	 * 查询快捷菜单
	 * @param userId 如果userId为null，表示查询系统的所有的快捷菜单，否则查询某个用户的快捷菜单
	 * @return
	 */
	private List<AcUserShortcutForm> searchShortcuts(Long userId){
		List<Object[]> tempList = null;
		
		if(userId == null){
			String hql = "select shortcut, menu from AcUserShortcut shortcut, AcMenu menu where shortcut.menuId = menu.menuId order by shortcut.sortNo asc";
			tempList = acUserShortcutDao.find(hql);
		}
		else{
			String hql = "select shortcut, menu from AcUserShortcut shortcut, AcMenu menu where shortcut.menuId = menu.menuId and shortcut.userId = ? order by shortcut.sortNo asc";
			tempList = acUserShortcutDao.find(hql, userId);
		}
		
		return AcUserShortcutConverter.getShortcutFormList(tempList);
	}
	
	/**
	 * 获取所有的快捷菜单
	 * @return
	 */
	public List<AcUserShortcutForm> getShortcuts(){
		return searchShortcuts(null);
	}
	
	/**
	 * 获取用户的快捷菜单
	 * @param userId 用户id
	 */
	public List<AcUserShortcutForm> getShortcuts(Long userId){
		return searchShortcuts(userId);
	}
	
	/**
	 * 获取拥有指定快捷菜单的用户
	 * @param menuId 菜单id
	 * @return
	 */
	public Long[] getUserIdsOwnedShortcuts(Long menuId){
		if(menuId != null){
			return acUserShortcutDao.getUserIdsOwnedShortcut(menuId);
		}
		return null;
	}
	
	/**
	 * 分页获取用户的菜单
	 * @param page
	 * @param userId
	 * @return
	 */
	public Page<AcUserShortcutForm> getShortcuts(Page<AcUserShortcutForm> page){
		List<AcUserShortcutForm> result = null;
		Page pg = PageUtils.newPageInstance(page);
		String hql = "select shortcut, menu from AcUserShortcut shortcut, AcMenu menu where shortcut.menuId = menu.menuId order by shortcut.sortNo asc";
		acUserShortcutDao.findPage(pg, hql);
		
		List<Object[]> tempList = pg.getResult();
		result = AcUserShortcutConverter.getShortcutFormList(tempList);
		PageUtils.copy(pg, result, page);
		return page;
	}
	
	/**
	 * 分页获取用户的菜单
	 * @param page
	 * @param userId
	 * @return
	 */
	public Page<AcUserShortcutForm> getShortcuts(Page<AcUserShortcutForm> page, AcUserShortcutForm form, Long userId){
		if(userId != null){
			List<AcUserShortcutForm> result = null;
			Page pg = PageUtils.newPageInstance(page);
			StringBuffer hql = new StringBuffer("select shortcut, menu from AcUserShortcut shortcut, AcMenu menu where shortcut.menuId = menu.menuId and shortcut.userId = ?");
			List values = new ArrayList();
			values.add(userId);
			if(form != null){
				if(StringUtils.isNotBlank(form.getMenuCode().trim())){
					hql.append(" and menu.menuCode like ?");
					values.add("%" + form.getMenuCode() + "%");
				}
				if(StringUtils.isNotBlank(form.getMenuName().trim())){
					hql.append(" and menu.menuName like ?");
					values.add("%" + form.getMenuName() + "%");
				}
			}
			if( page.getOrderBy().equals("sortNo") ){
				hql.append(" order by shortcut.sortNo asc");
			}
			else{
				String orderBy = HqlUtils.buildOrderBy(page, "menu");
				hql.append(orderBy);
			}
			acUserShortcutDao.findPage(pg, hql.toString(), values);
			
			List<Object[]> tempList = pg.getResult();
			result = AcUserShortcutConverter.getShortcutFormList(tempList);
			PageUtils.copy(pg, result, page);
			return page;
		}
		return null;
	}
}
